package cn.agiledata.bank.linkbank.service;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.agiledata.bank.common.servlet.InitForString;
import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.common.util.IOUtil;
import cn.agiledata.bank.common.util.StringUtil;
import cn.agiledata.bank.hibernateBean.Sysbook;
import cn.agiledata.bank.linkbank.exception.LinkBankException;
import cn.agiledata.bank.linkbank.logic.payment.BankTelPayRequest;
import cn.agiledata.bank.linkbank.logic.payment.BankTelPayRequestFeekback;
import cn.agiledata.bank.linkbank.logic.payment.UnsPayRequest;

/**
 * 
 * @author maxianbo
 * 
 */
public class BankTelPayServiceImpl extends BankPayServiceImpl implements
        BankTelPayService {
    private Map requestFeekbackMapping;

    public void setRequestFeekbackMapping(Map requestFeekbackMapping) {
        this.requestFeekbackMapping = requestFeekbackMapping;
    }

    public boolean validTelBankCode(String bankCode) {
        return validBankCode(getSupportedBanks(), bankCode);
    }

    private int getDefaultExpireDays() {
        try {
            List sysbooks = (List) InitForString.dataDictMap
                    .get(EXPIRE_DAYS_KEY);
            Sysbook sysbook = (Sysbook) sysbooks.get(0);
            String days = sysbook.getId().getListId();
            return Integer.parseInt(days);
        } catch (Exception e) {
            return 0;
        }
    }

    public BankTelPayRequest getBankTelPayRequest(UnsPayRequest unsPayRequest,
            String transId, String orderTelephone, String webRootUrl)
            throws LinkBankException {
        String bankCode = unsPayRequest.getBank().getBankCode();
        BankTelPayRequest bankTelPayRequest = (BankTelPayRequest) getBankPayRequest(
                bankCode, webRootUrl);
        if (bankTelPayRequest == null) {
            throw LinkBankException.UNSUPPORTED_TEL_BANK;
        }
        bankTelPayRequest.setOrderId(transId);
        bankTelPayRequest.setAmount(unsPayRequest.getAmount());
        bankTelPayRequest.setCurrencyType(unsPayRequest.getCurrencyType());
        Date date = unsPayRequest.getTime();
        bankTelPayRequest.setDate(date);
        Date expireDate = DateUtil.addDay(date, getDefaultExpireDays());
        bankTelPayRequest.setExpireDate(expireDate);
        bankTelPayRequest.setOrderTelephone(orderTelephone);

        bankTelPayRequest.prepare();

        return bankTelPayRequest;
    }

    private BankTelPayRequestFeekback getBankTelPayRequestFeekback(
            String bankCode, String feekback) throws LinkBankException {
        BankTelPayRequestFeekback requestFeekback = (BankTelPayRequestFeekback) this.requestFeekbackMapping
                .get(bankCode);
        if (requestFeekback == null)
            return null;
        requestFeekback = requestFeekback.copy();
        requestFeekback.setFeekback(feekback);
        return requestFeekback;
    }

    public BankTelPayRequestFeekback sendBankTelPayRequest(String bankCode,
            BankTelPayRequest bankTelPayRequest) throws LinkBankException {
        try {
            String url = bankTelPayRequest.getBankUrl();
            Map parameters = bankTelPayRequest.getParameters();
            String feekback = IOUtil.postUrl(url, parameters);
            return getBankTelPayRequestFeekback(bankCode, feekback);
        } catch (IOException e) {
            throw LinkBankException.CANNOT_LINK_BANK;
        }
    }

    public void validateOrderTelephone(String orderTelephone)
            throws LinkBankException {
        if (!StringUtil.isFixedTel(orderTelephone)
                && !StringUtil.isMobile(orderTelephone)) {
            throw LinkBankException.INVALID_ORDER_TELPHONE;
        }
    }
}
